package com.mftour.moudles.controller;

import java.io.File;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.mftour.common.Constant;
import com.mftour.moudles.entity.AccountBook;
import com.mftour.moudles.service.AccountBookService;
import com.mftour.moudles.vo.AjaxResponse;
import com.mftour.moudles.vo.ShowPage;
import com.mftour.moudles.vo.search.SearchAccountBookPagingVo;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * This class was generated by Bill Generator. This class corresponds to the
 * database table account_books 台账
 *
 * @mbggenerated 2017-56-19 11:56
 */
@Controller
@RequestMapping(value = "/accountBook")
public class AccountBookController extends BaseController {

	@Autowired
	AccountBookService accountBookSer;

	@RequestMapping(value = "/ajax/list", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "根据条件获取台账列表", notes = "返回台账列表")
	public ShowPage getAccountBookList(SearchAccountBookPagingVo searchAccountBookPagingVo) {
		List<AccountBook> accountBookList = accountBookSer.selectListSelectivePaging(searchAccountBookPagingVo);
		ShowPage page = new ShowPage(searchAccountBookPagingVo, accountBookList);
		return page;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ApiOperation(value = "根据条件获取台账列表", notes = "返回台账列表")
	public ModelAndView toAccountBookList(SearchAccountBookPagingVo searchAccountBookPagingVo) {
		ModelAndView mv = new ModelAndView("accountBook/accountBookList");
		List<AccountBook> accountBookList = accountBookSer.selectListSelectivePaging(searchAccountBookPagingVo);
		ShowPage page = new ShowPage(searchAccountBookPagingVo, accountBookList);
		mv.addObject("page", page);
		mv.addObject("searchAccountBookPagingVo", searchAccountBookPagingVo);
		return mv;
	}

	@RequestMapping(value = "/ajax/save", method = RequestMethod.POST)
	@ResponseBody
	 @Validated 
	@ApiOperation(value = "新增台账", notes = "新增台账")
	@ApiImplicitParams(value = { @ApiImplicitParam(required = false, name = "id", paramType = "query") })
	public AjaxResponse<AccountBook> save(AccountBook accountBook) {
		accountBook.setId(null);
		AjaxResponse<AccountBook> ajaxResponse = new AjaxResponse<AccountBook>(Constant.RS_CODE_ERROR, "添加台账出错！");

		accountBookSer.insertSelective(accountBook);
		ajaxResponse.setCode(Constant.RS_CODE_SUCCESS);
		ajaxResponse.setMsg("添加台账成功！");
		return ajaxResponse;
	}

	@RequestMapping(value = "/ajax/del", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "根据pk删除台账", notes = "根据删除台账")
	@ApiImplicitParams(value = { @ApiImplicitParam(required = true, name = "id", value = "pk", paramType = "query") })
	public AjaxResponse<Object> del(Integer id) {
		AjaxResponse<Object> ajaxResponse = new AjaxResponse<Object>(Constant.RS_CODE_ERROR, "pk不能为null！");
		if (id == null) {
			return ajaxResponse;
		}

		accountBookSer.deleteByPrimaryKey(id);
		ajaxResponse.setCode(Constant.RS_CODE_SUCCESS);
		ajaxResponse.setMsg("删除台账成功！");
		return ajaxResponse;
	}

	@RequestMapping(value = "/ajax/delAll", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "根据pk集合删除台账", notes = "根据pk集合删除台账，以','为分隔符")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(required = true, name = "ids", value = "pk集合", paramType = "query", allowMultiple = true, dataType = "Integer") })
	public AjaxResponse<Object> delAll(Integer[] ids) {
		AjaxResponse<Object> ajaxResponse = new AjaxResponse<Object>(Constant.RS_CODE_ERROR, "请选择删除项！");
		if (ids == null || ids.length < 0) {
			return ajaxResponse;
		}

		accountBookSer.deleteByPKs(ids);
		ajaxResponse.setCode(Constant.RS_CODE_SUCCESS);
		ajaxResponse.setMsg("删除台账成功!");
		return ajaxResponse;
	}

	@RequestMapping(value = "/ajax/update", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "根据pk更新台账", notes = "根据pk更新台账，属性为null的不更新")
	public AjaxResponse<Object> update(AccountBook accountBook) {
		if (accountBook.getId() == null) {
			return new AjaxResponse<Object>(Constant.RS_CODE_ERROR, "请设置pk！");
		}
		AjaxResponse<Object> ajaxResponse = new AjaxResponse<Object>(Constant.RS_CODE_ERROR, "修改台账出错！");
		accountBookSer.updateByPrimaryKeySelective(accountBook);
		ajaxResponse.setCode(Constant.RS_CODE_SUCCESS);
		ajaxResponse.setMsg("修改台账成功！");
		return ajaxResponse;
	}

	@RequestMapping(value = "/ajax/entity", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "根据pk获取台账", notes = "根据pk获取台账")
	public AjaxResponse<AccountBook> getEntity(Integer id) {
		if (id == null) {
			return new AjaxResponse<AccountBook>(Constant.RS_CODE_ERROR, "请选择pk！");
		}
		AccountBook accountBook = accountBookSer.selectByPrimaryKey(id);
		AjaxResponse<AccountBook> ajaxResponse = new AjaxResponse<AccountBook>(Constant.RS_CODE_SUCCESS, "获取台账成功！");
		if (accountBook != null) {
			ajaxResponse.setData(accountBook);
		} else {
			ajaxResponse.setCode(Constant.RS_CODE_ERROR);
			ajaxResponse.setMsg("获取台账失败！");
		}
		return ajaxResponse;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public boolean parseFile(File file, AjaxResponse ajaxResponse) {
		if (accountBookSer.parseAccountBookExcel(file)) {
			ajaxResponse.setCode(Constant.RS_CODE_SUCCESS);
			ajaxResponse.setMsg("上传成功");
			return true;
		} else {
			ajaxResponse.setCode(Constant.RS_CODE_ERROR);
			ajaxResponse.setMsg("解析文件失败!");
			return false;
		}

	}

}